package com.exam.sacrifice.ui;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.media.MediaPlayer;
import android.net.Uri;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.exam.commonbiz.base.BaseActivity;
import com.exam.commonbiz.base.BaseViewModel;
import com.exam.commonbiz.util.CommonCallBack;
import com.exam.commonbiz.util.OnFastClickListener;
import com.exam.commonbiz.util.StatusBarUtil;
import com.exam.commonbiz.util.ToastUtil;
import com.exam.sacrifice.R;
import com.exam.sacrifice.databinding.ActivityJiSiBinding;
import com.exam.sacrifice.service.RawMusicService;
import com.exam.sacrifice.ui.dialog.JiBaiDialog;

public class JiSiActivity extends BaseActivity<BaseViewModel, ActivityJiSiBinding> {

    private boolean gongxifacaiPlayed = false;
    private MediaPlayer mediaPlayerMusic;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ji_si;
    }

    @Override
    public void initData() {

        if (RawMusicService.getInstance().isPlaying()) {
            RawMusicService.getInstance().pauseMusic();
        }
        // 初始化MediaPlayer
        mediaPlayerMusic = MediaPlayer.create(this, R.raw.splash_music);
        mediaPlayerMusic.setLooping(true);
        mediaPlayerMusic.start();

        initJinRuCiTangVideo();
        binding.ivJibai.postDelayed(new Runnable() {
            @Override
            public void run() {
                initVideoBg();
                initShangXiangVideo();
            }
        }, 500);

        StatusBarUtil.setLightStatusBar(this, false, true);
        binding.include.tvTitle.setText("祭祀文化");
        binding.include.tvTitle.setTextColor(ContextCompat.getColor(context, R.color.white));
        binding.include.ivback.setImageDrawable(ContextCompat.getDrawable(context, com.exam.commonbiz.R.drawable.back_white));
        binding.include.ivback.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                back();
            }
        });

        binding.ivGongpin.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                actionStart(GongPinActivity.class);
            }
        });

        binding.ivJibai.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                new JiBaiDialog().show(context, new CommonCallBack() {
                    @Override
                    public void callback(int postion, Object object) {
                        int num = (int) object;
                        binding.shangxiangContainer.setVisibility(VISIBLE);
                        Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.shangxiang);
                        binding.videoViewShangXiang.setVideoURI(videoUri);
                        binding.videoViewShangXiang.start();
                        binding.tvJiwen.setVisibility(VISIBLE);
                    }
                });
            }
        });

        binding.ivJiwen.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                ToastUtil.getInstance().showToast("功能开发中");
            }
        });
    }

    @Override
    public void back() {
        if (binding.videoViewJinRuCiTang.isPlaying()) {
            binding.videoViewJinRuCiTang.setVisibility(GONE);
            binding.videoViewJinRuCiTang.stopPlayback();
            binding.llTopContainer.setVisibility(VISIBLE);

            if (mediaPlayerMusic != null) {
                mediaPlayerMusic.stop();
                mediaPlayerMusic.release();
                mediaPlayerMusic = null;
            }
            if (RawMusicService.getInstance().getCurrentPosition() > 0) {
                RawMusicService.getInstance().resumeMusic();
            }
            return;
        }
        if (binding.videoViewShangXiang.isPlaying()) {
            binding.shangxiangContainer.setVisibility(GONE);
            binding.videoViewShangXiang.stopPlayback();
            return;
        }
        finish();
    }

    @Override
    protected boolean isUseBlackFontWithStatusBar() {
        return false;
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mediaPlayerMusic != null) {
            mediaPlayerMusic.stop();
            mediaPlayerMusic.release();
            mediaPlayerMusic = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding.videoViewShangXiang.stopPlayback();
        binding.videoViewBg.stopPlayback();
        if (mediaPlayerMusic != null) {
            mediaPlayerMusic.stop();
            mediaPlayerMusic.release();
            mediaPlayerMusic = null;
        }
    }

    private void initVideoBg() {

        // 设置视频源 (res/raw/sample.mp4)
        Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.wuxiang);
        binding.videoViewBg.setVideoURI(videoUri);

        // 设置监听器
        binding.videoViewBg.setOnPreparedListener(mp -> {
            binding.videoViewBg.start();
        });

        binding.videoViewBg.setOnCompletionListener(mp -> {
            binding.videoViewBg.start();
        });

        binding.videoViewBg.setOnErrorListener((mp, what, extra) -> {
            ToastUtil.getInstance().showToast("播放错误");
            return true;
        });
    }

    private void initJinRuCiTangVideo() {

        // 设置视频源 (res/raw/sample.mp4)
        Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.jinrucitang);
        binding.llTopContainer.setVisibility(GONE);
        binding.videoViewJinRuCiTang.setVisibility(VISIBLE);
        binding.videoViewJinRuCiTang.setVideoURI(videoUri);

        // 设置监听器
        binding.videoViewJinRuCiTang.setOnPreparedListener(mp -> {
            binding.videoViewJinRuCiTang.start();
        });

        binding.videoViewJinRuCiTang.setOnCompletionListener(mp -> {
            binding.videoViewJinRuCiTang.setVisibility(GONE);
            binding.llTopContainer.setVisibility(VISIBLE);
            if (mediaPlayerMusic != null) {
                mediaPlayerMusic.stop();
                mediaPlayerMusic.release();
                mediaPlayerMusic = null;
            }
            if (RawMusicService.getInstance().getCurrentPosition() > 0) {
                RawMusicService.getInstance().resumeMusic();
            }
        });

        binding.videoViewJinRuCiTang.setOnErrorListener((mp, what, extra) -> {
            ToastUtil.getInstance().showToast("播放错误");
            return true;
        });
    }

    private void initShangXiangVideo() {

        // 设置视频源 (res/raw/sample.mp4)
        Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.shangxiang);
        binding.shangxiangContainer.setVisibility(GONE);
        binding.videoViewShangXiang.setVideoURI(videoUri);

        // 设置监听器
        binding.videoViewShangXiang.setOnPreparedListener(mp -> {

        });

        binding.videoViewShangXiang.setOnCompletionListener(mp -> {
            if (gongxifacaiPlayed) {
                binding.shangxiangContainer.setVisibility(GONE);
                Uri videoUri2 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.youxiang);
                binding.videoViewBg.setVideoURI(videoUri2);
                binding.videoViewBg.start();
            } else {
                binding.tvJiwen.setVisibility(GONE);
                Uri videoUri2 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.gongxifacai);
                binding.videoViewShangXiang.setVideoURI(videoUri2);
                binding.videoViewShangXiang.start();
                gongxifacaiPlayed = true;
            }
        });

        binding.videoViewShangXiang.setOnErrorListener((mp, what, extra) -> {
            ToastUtil.getInstance().showToast("播放错误");
            return true;
        });
    }
}